#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/main.h"
#include"../include/unmount_vfs.h"
#include"../include/main_header.h"
#include"../include/narrtree.h"
#include"../include/hash.h"
#include"../include/constants.h"
#include"../include/makedir.h"
#include"../include/mount_vfs.h"
#include"../include/create_file.h"
#include"../include/vfs_errorcodes.h"
#include"../include/create_vfs.h"

FILE *vfs;
int file_counter;

int unmountvfs(char vfs_name[])
{
	size_t size;
	int used_fd;
	int i,j=1,no_of_one[MAX_FD],k,counter=0,l=0,to_seek,flag,counter1;
	fdesc array_fd[MAX_FD],*t;
	char current_data[DATA_SIZE],*tf;
	vfs = fopen(vfs_name,"r+b");
	if(vfs != NULL)
	{
		size=fread(&flag,INT_SIZE,1,vfs);
		
		tf=(char *)malloc(sizeof(char)*MAX_NAME);
		size=fread(tf,MAX_NAME,1,vfs);
		if(flag==0)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UNMOUNT_03);//add error code
		else 
		{
			flag=0;
			fseek(vfs,0,SEEK_SET);
			size=fwrite(&flag,INT_SIZE,1,vfs);
			if(size==0)
				printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UNMOUNT_02);
			else
			{
				fseek(vfs,INT_SIZE+MAX_NAME,SEEK_CUR);
				size=fread(&used_fd,INT_SIZE,1,vfs);
				if(used_fd != local_used_fd)
				{
					fseek(vfs,MAX_NAME+2*INT_SIZE,SEEK_SET);	
					size=fwrite(&local_used_fd,INT_SIZE,1,vfs);//no of used files
					update_fd(head);
					fseek(vfs,MAX_NAME+4*INT_SIZE+size_of_vfs*sizeof(fdesc),SEEK_SET);
					for(i=1;i<data_num;i++)
					{
						fseek(vfs,MAX_NAME+3*INT_SIZE+size_of_vfs*sizeof(fdesc)+size_of_vfs*INT_SIZE,SEEK_SET);
						if(*(block_arr+i)!=0 && *(block_arr+i) < size_of_vfs)
						{
							while(data[i][l] != '\0')
								current_data[l]=data[i][l++];
							current_data[l]='\0';
							fseek(vfs,(*(block_arr+i))*BLOCK_SIZE,SEEK_CUR);
							size=fwrite(current_data,BLOCK_SIZE,1,vfs);
						}
						l=0;
					}
					i=1;
					counter=1;
					counter1=0;
					fseek(vfs,MAX_NAME+4*INT_SIZE+size_of_vfs*sizeof(fdesc),SEEK_SET);
					while(*(block_arr+i)!=0)
					{
						if(*(block_arr+i) < size_of_vfs)
							size=fwrite(&counter,INT_SIZE,1,vfs);
						else
							size=fwrite(&counter1,INT_SIZE,1,vfs);
						i++;
					}	
					//close(vfs);
				}
				//traverse(head);
				printf("unmountvfs_SUCCESS\n");
				mounted=0;
			}
		}
	}
	else
	{
		if(mounted == 0)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UNMOUNT_03);
		else		
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UNMOUNT_01);
	}
	if(vfs!=NULL)
	close(vfs);
}

void update_fd(ntree *head)
{
	ntree *temp4,*q;
	q=head;
	size_t size;
	if(q!=NULL)
	{
		temp4=q;
		do
		{
			size=fwrite(temp4->file_desc->filename,MAX_NAME,1,vfs);
			size=fwrite(temp4->file_desc->filepath,MAX_PATH,1,vfs);
			size=fwrite(&temp4->file_desc->filetype,INT_SIZE,1,vfs);
			size=fwrite(&temp4->file_desc->file_size,INT_SIZE,1,vfs);
			size=fwrite(&temp4->file_desc->blk_num,INT_SIZE,1,vfs);
			if(temp4->file_desc->filetype == 0)
				file_counter++;
			update_fd(temp4->first);
			temp4=temp4->next;
		}while(temp4!=NULL);
		q=q->first;
	}
}
